package com.hutiao.snowhb.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hutiao.snowhb.common.DateTimeUtil;
import com.hutiao.snowhb.dao.BaseDao;
import com.hutiao.snowhb.entities.SnowMMLocation;
import com.hutiao.snowhb.entities.SnowMM;
import com.hutiao.snowhb.entities.SnowMMComment;
import com.hutiao.snowhb.entities.SnowMMPrebook;
import com.hutiao.snowhb.entities.UserInfo;
import com.hutiao.snowhb.entities.UserLocation;
import com.hutiao.snowhb.entities.WeixinUserInfo;
import com.hutiao.snowhb.resource.ConfigInfo;

@Component
public class SnowMMService {
	
	private static Logger logger = Logger.getLogger(SnowMMService.class);
	
	@Autowired
	private BaseDao baseDao;
	
	
	
	public SnowMMLocation getLocationByMlid(int mlid) {
		
		String sql = "select mlid,mmid,latitude,longitude,label,poi,checkinTime,checkoutTime,onlineTime,`status` "
				+ "from snowmm_mmlocation where mlid="+mlid+" ";
		
		Map<String, Object> temp = this.baseDao.queryForMap(sql);
		
		if (temp == null) {
			return null;
		}
		
		SnowMMLocation loca = new SnowMMLocation();
		loca.setMlid(Integer.parseInt(temp.get("mlid").toString()));
		loca.setMmid(Integer.parseInt(temp.get("mmid").toString()));
		loca.setLatitude(Double.parseDouble(temp.get("latitude").toString()));
		loca.setLongitude(Double.parseDouble(temp.get("longitude").toString()));
		loca.setLabel(temp.get("label").toString());
		loca.setPoi(temp.get("poi").toString());
		loca.setCheckinTime(DateTimeUtil.String2Date2(temp.get("checkinTime").toString()));
		loca.setCheckoutTime(temp.get("checkoutTime")==null ? null : DateTimeUtil.String2Date2(temp.get("checkoutTime").toString()));
		loca.setOnlineTime(temp.get("onlineTime")==null ? -1 : Integer.parseInt(temp.get("onlineTime").toString()));
		loca.setStatus(Integer.parseInt(temp.get("status").toString()));
		
		return loca;
	}
	

	
	/**
	 * 根据mmid获取mm当前所在的位置
	 * @param mmid
	 * @return
	 */
	public SnowMMLocation getCurrentLocation(int mmid) {
		
		String sql = "select mlid,mmid,latitude,longitude,label,poi,checkinTime,checkoutTime,onlineTime,`status` "
				+ "from snowmm_mmlocation where `status`=0 and mmid="+mmid+" ";
		
		Map<String, Object> temp = this.baseDao.queryForMap(sql);
		
		if (temp == null) {
			return null;
		}
		
		SnowMMLocation loca = new SnowMMLocation();
		loca.setMlid(Integer.parseInt(temp.get("mlid").toString()));
		loca.setMmid(Integer.parseInt(temp.get("mmid").toString()));
		loca.setLatitude(Double.parseDouble(temp.get("latitude").toString()));
		loca.setLongitude(Double.parseDouble(temp.get("longitude").toString()));
		loca.setLabel(temp.get("label").toString());
		loca.setPoi(temp.get("poi").toString());
		loca.setCheckinTime(DateTimeUtil.String2Date2(temp.get("checkinTime").toString()));
		loca.setCheckoutTime(temp.get("checkoutTime")==null ? null : DateTimeUtil.String2Date2(temp.get("checkoutTime").toString()));
		loca.setOnlineTime(temp.get("onlineTime")==null ? -1 : Integer.parseInt(temp.get("onlineTime").toString()));
		loca.setStatus(Integer.parseInt(temp.get("status").toString()));
		
		return loca;
	}
	
	
	/**
	 * 获取单条mm评论
	 * @param mcid mm评论id
	 * @return
	 */
	public SnowMMComment getComment(int mcid) {
		
		String sql = "select mcid,mmid,starValue,comment,createTime from snowmm_mmcomment where mcid="+mcid+";";
		
		Map<String, Object> temp = this.baseDao.queryForMap(sql);
		
		if (temp == null) {
			return null;
		}
		
		SnowMMComment comment = new SnowMMComment();
		comment.setMcid(mcid);
		comment.setMmid(Integer.parseInt(temp.get("mmid").toString()));
		comment.setStarValue(Integer.parseInt(temp.get("starValue").toString()));
		comment.setContent(temp.get("comment").toString());
		comment.setCreateTime(DateTimeUtil.String2Date2(temp.get("createTime").toString()));
		
		return comment;
	}
	
	
	/**
	 * 根据mmid获取该mm的所有评论
	 * @param mmid
	 * @return
	 */
	public List<SnowMMComment> getComments(int mmid) {
		
		String sql = "select c.mcid,c.mmid,c.reviewer,c.starValue,c.content,c.createTime, "
				+ "u.uid,u.subscribe,u.openId,u.nickname,u.sex,u.city,u.country,u.province,u.language,u.headimgurl,u.subscribeTime "
				+ "from snowmm_mmcomment c left join snow_user u on c.reviewer=u.openId "
				+ "where c.mmid="+mmid+";";
		
		List<Map<String, Object>> tempList = this.baseDao.queryForList(sql);
		
		if (tempList == null || tempList.size() == 0) {
			return null;
		}
		
		List<SnowMMComment> comments = new ArrayList<SnowMMComment>();
		for (Map<String, Object> temp : tempList) {
			SnowMMComment comment = new SnowMMComment();
			comment.setMcid(Integer.parseInt(temp.get("mcid").toString()));
			comment.setMmid(mmid);
			comment.setReviewer(temp.get("reviewer").toString());
			comment.setStarValue(Integer.parseInt(temp.get("starValue").toString()));
			comment.setContent(temp.get("content").toString());
			comment.setCreateTime(DateTimeUtil.String2Date2(temp.get("createTime").toString()));
			
			int uid = temp.get("uid") == null ? -1 : Integer.parseInt(temp.get("uid").toString());
			UserInfo user = new UserInfo();
			user.setUid(uid);
			WeixinUserInfo wxUserInfo = new WeixinUserInfo();
			
			if (uid != -1) {
				int sub = Integer.parseInt(temp.get("subscribe").toString());
				wxUserInfo.setSubscribe(sub);
				wxUserInfo.setOpenId(temp.get("openId").toString());
				wxUserInfo.setNickname(sub==0 ? null : temp.get("nickname").toString());
				wxUserInfo.setSex(sub==0 ? 0 : Integer.parseInt(temp.get("sex").toString()));
				wxUserInfo.setCity(sub==0 ? null : temp.get("city").toString());
				wxUserInfo.setCountry(sub==0 ? null : temp.get("country").toString());;
				wxUserInfo.setProvince(sub==0 ? null : temp.get("province").toString());
				wxUserInfo.setLanguage(sub==0 ? null : temp.get("language").toString());
				wxUserInfo.setHeadimgurl(sub==0 ? null : temp.get("headimgurl").toString());
				//wxUserInfo.setSubscribe_time(sub==0 ? 0 : Long.parseLong(temp.get("subscribe_time").toString()));
				user.setWxUserInfo(wxUserInfo);
			} else {
				wxUserInfo.setHeadimgurl(ConfigInfo.wxHeadImg);
				wxUserInfo.setHeadimgurl64(ConfigInfo.wxHeadImg64);
				wxUserInfo.setNickname("anonym");
				user.setWxUserInfo(wxUserInfo);
			}
			
			if (user.getWxUserInfo().getHeadimgurl() == null || user.getWxUserInfo().getHeadimgurl().equals("")) {
				user.getWxUserInfo().setHeadimgurl(ConfigInfo.wxHeadImg);
				user.getWxUserInfo().setHeadimgurl64(ConfigInfo.wxHeadImg64);
			}
			
			comment.setReviewerInfo(user);
			comments.add(comment);
		}
		
		return comments;
	}
	
	/**
	 * 根据openId获取mm资料
	 * @param openId
	 * @return
	 */
	public SnowMM getMMbyOpenId(String openId) {
		
		String sql = "select mmid,openId,name,nickname,phone,age,constellation,introduce,hobby,photoUrl,photoUrlBig, "
				+ "sellDistrict,sellOffice,rank,starAvg,commentCount,mlid,createTime,updateTime,lastTime "
				+ "from snowmm_mminfo where openId='"+openId+"';";
		
		Map<String, Object> temp = this.baseDao.queryForMap(sql);
		
		if (temp == null) {
			return null;
		}
		
		SnowMM snowmm = new SnowMM();
		snowmm.setMmid(Integer.parseInt(temp.get("mmid").toString()));
		snowmm.setOpenId(temp.get("openId")==null ? null : temp.get("openId").toString());
		snowmm.setName(temp.get("name").toString());
		snowmm.setNickname(temp.get("nickname").toString());
		snowmm.setPhone(temp.get("phone")==null ? null : temp.get("phone").toString());
		snowmm.setAge(Integer.parseInt(temp.get("age").toString()));
		snowmm.setConstellation(temp.get("constellation").toString());
		snowmm.setIntroduce(temp.get("introduce").toString());
		snowmm.setHobby(temp.get("hobby").toString());
		snowmm.setPhotoUrl(temp.get("photoUrl").toString());
		snowmm.setPhotoUrlBig(temp.get("photoUrlBig").toString());
		snowmm.setSellDistrict(temp.get("sellDistrict").toString());
		snowmm.setSellOffice(temp.get("sellOffice").toString());
		snowmm.setRank(Integer.parseInt(temp.get("rank").toString()));
		snowmm.setStarAvg(Float.parseFloat(temp.get("starAvg").toString()));
		snowmm.setCommentCount(Integer.parseInt(temp.get("commentCount").toString()));
		
		return snowmm;
	}
	
	
	public SnowMM getMMById(int mmid) {
		
		String sql = "select mmid,openId,name,nickname,phone,age,constellation,introduce,hobby,photoUrl,photoUrlBig, "
				+ "sellDistrict,sellOffice,rank,starAvg,commentCount,mlid,createTime,updateTime,lastTime "
				+ "from snowmm_mminfo where mmid="+mmid+";";
		
		Map<String, Object> temp = this.baseDao.queryForMap(sql);
		
		if (temp == null) {
			return null;
		}
		
		SnowMM snowmm = new SnowMM();
		snowmm.setMmid(Integer.parseInt(temp.get("mmid").toString()));
		snowmm.setOpenId(temp.get("openId")==null ? null : temp.get("openId").toString());
		snowmm.setName(temp.get("name").toString());
		snowmm.setNickname(temp.get("nickname").toString());
		snowmm.setPhone(temp.get("phone")==null ? null : temp.get("phone").toString());
		snowmm.setAge(Integer.parseInt(temp.get("age").toString()));
		snowmm.setConstellation(temp.get("constellation").toString());
		snowmm.setIntroduce(temp.get("introduce").toString());
		snowmm.setHobby(temp.get("hobby").toString());
		snowmm.setPhotoUrl(temp.get("photoUrl").toString());
		snowmm.setPhotoUrlBig(temp.get("photoUrlBig").toString());
		snowmm.setSellDistrict(temp.get("sellDistrict").toString());
		snowmm.setSellOffice(temp.get("sellOffice").toString());
		snowmm.setRank(Integer.parseInt(temp.get("rank").toString()));
		snowmm.setStarAvg(Float.parseFloat(temp.get("starAvg").toString()));
		snowmm.setCommentCount(Integer.parseInt(temp.get("commentCount").toString()));
		
		return snowmm;
	}
	
	
	public List<SnowMM> getMMList() {
		
		String sql = "select mmid,openId,name,nickname,phone,age,constellation,introduce,hobby,photoUrl,photoUrlBig, "
				+ "sellDistrict,sellOffice,rank,starAvg,commentCount,mlid,createTime,updateTime,lastTime "
				+ "from snowmm_mminfo order by rank asc;";
		
		List<Map<String, Object>> tempList = this.baseDao.queryForList(sql);
		
		if (tempList == null || tempList.size() == 0) {
			return null;
		}
		
		List<SnowMM> mmList = new ArrayList<SnowMM>();
		for (Map<String, Object> temp : tempList) {
			SnowMM snowmm = new SnowMM();
			snowmm.setMmid(Integer.parseInt(temp.get("mmid").toString()));
			snowmm.setOpenId(temp.get("openId")==null ? null : temp.get("openId").toString());
			snowmm.setName(temp.get("name").toString());
			snowmm.setNickname(temp.get("nickname").toString());
			snowmm.setPhone(temp.get("phone")==null ? null : temp.get("phone").toString());
			snowmm.setAge(Integer.parseInt(temp.get("age").toString()));
			snowmm.setConstellation(temp.get("constellation").toString());
			snowmm.setIntroduce(temp.get("introduce").toString());
			snowmm.setHobby(temp.get("hobby").toString());
			snowmm.setPhotoUrl(temp.get("photoUrl").toString());
			snowmm.setPhotoUrlBig(temp.get("photoUrlBig").toString());
			snowmm.setSellDistrict(temp.get("sellDistrict").toString());
			snowmm.setSellOffice(temp.get("sellOffice").toString());
			snowmm.setRank(Integer.parseInt(temp.get("rank").toString()));
			snowmm.setStarAvg(Float.parseFloat(temp.get("starAvg").toString()));
			snowmm.setCommentCount(Integer.parseInt(temp.get("commentCount").toString()));
			mmList.add(snowmm);
		}
		
		return mmList;
	}
	
	
	public List<SnowMM> getMMLocaList() {
		
		String sql = "select m.mmid,m.openId,m.name,m.nickname,m.phone,m.age,m.constellation,m.introduce,m.hobby,m.photoUrl,m.photoUrlBig, "
				+ "m.sellDistrict,m.sellOffice,m.rank,m.starAvg,m.commentCount,m.createTime,m.updateTime,m.lastTime, "
				+ "l.mlid,l.latitude,l.longitude,l.label,l.poi,l.checkinTime,l.checkoutTime,l.onlineTime,l.`status` "
				+ "from snowmm_mminfo m left join snowmm_mmlocation l on m.mmid=l.mmid and l.`status`=0 "
				+ "order by m.rank asc;";
		
		List<Map<String, Object>> tempList = this.baseDao.queryForList(sql);
		
		if (tempList == null || tempList.size() == 0) {
			return null;
		}
		
		List<SnowMM> mmList = new ArrayList<SnowMM>();
		for (Map<String, Object> temp : tempList) {
			SnowMM snowmm = new SnowMM();
			snowmm.setMmid(Integer.parseInt(temp.get("mmid").toString()));
			snowmm.setOpenId(temp.get("openId")==null ? null : temp.get("openId").toString());
			snowmm.setName(temp.get("name").toString());
			snowmm.setNickname(temp.get("nickname").toString());
			snowmm.setPhone(temp.get("phone")==null ? null : temp.get("phone").toString());
			snowmm.setAge(Integer.parseInt(temp.get("age").toString()));
			snowmm.setConstellation(temp.get("constellation").toString());
			snowmm.setIntroduce(temp.get("introduce").toString());
			snowmm.setHobby(temp.get("hobby").toString());
			snowmm.setPhotoUrl(temp.get("photoUrl").toString());
			snowmm.setPhotoUrlBig(temp.get("photoUrlBig").toString());
			snowmm.setSellDistrict(temp.get("sellDistrict").toString());
			snowmm.setSellOffice(temp.get("sellOffice").toString());
			snowmm.setRank(Integer.parseInt(temp.get("rank").toString()));
			snowmm.setStarAvg(Float.parseFloat(temp.get("starAvg").toString()));
			snowmm.setCommentCount(Integer.parseInt(temp.get("commentCount").toString()));
			
			SnowMMLocation loca = null;
			if (temp.get("mlid") != null) {
				loca = new SnowMMLocation();
				loca.setMlid(Integer.parseInt(temp.get("mlid").toString()));
				loca.setMmid(Integer.parseInt(temp.get("mmid").toString()));
				loca.setLatitude(Double.parseDouble(temp.get("latitude").toString()));
				loca.setLongitude(Double.parseDouble(temp.get("longitude").toString()));
				loca.setLabel(temp.get("label").toString());
				loca.setPoi(temp.get("poi").toString());
				loca.setCheckinTime(DateTimeUtil.String2Date2(temp.get("checkinTime").toString()));
				loca.setCheckoutTime(temp.get("checkoutTime")==null ? null : DateTimeUtil.String2Date2(temp.get("checkoutTime").toString()));
				loca.setOnlineTime(temp.get("onlineTime")==null ? -1 : Integer.parseInt(temp.get("onlineTime").toString()));
				loca.setStatus(Integer.parseInt(temp.get("status").toString()));
			} 
			
			snowmm.setCurrLocation(loca);
			
			mmList.add(snowmm);
		}
		
		return mmList;
	}
	
	
	
	
	
	
	/**
	 * 保存mm评论
	 * @param comment
	 * @return
	 * @throws SQLException
	 */
	public int saveComment(SnowMMComment comment) throws SQLException {
		
		int commentCount = 1;
		float starAvg = comment.getStarValue();
		
		String sql = "select sum(starValue) as totalStarValue,count(*) as commentCount "
				+ "from snowmm_mmcomment where mmid="+comment.getMmid()+" group by mmid;";
		Map<String, Object> temp = this.baseDao.queryForMap(sql);
		
		if (temp != null) {
			commentCount += Integer.parseInt(temp.get("commentCount").toString());
			starAvg = (starAvg + Float.parseFloat(temp.get("totalStarValue").toString())) / commentCount;
		}
		
		sql = "insert into snowmm_mmcomment(mmid,reviewer,starValue,content,createTime) "
				+ "values("+comment.getMmid()+",'"+comment.getReviewer()+"',"+comment.getStarValue()+",'"+comment.getContent()+"',now());";
		int mcid = this.baseDao.save(sql);
		
		
		sql = "update snowmm_mminfo set starAvg="+starAvg+",commentCount="+commentCount+" where mmid="+comment.getMmid()+";";
		this.baseDao.execute(sql);
		
		return mcid;
		
	}
	
	/**
	 * 删除mm的某一条评论
	 * @param comment
	 */
	public void delComment(SnowMMComment comment) {
		
		int commentCount = 0;
		float starAvg = 0;
		
		String sql = "select sum(starValue) as totalStarValue,count(*) as commentCount "
				+ "from snowmm_mmcomment where mmid="+comment.getMmid()+" group by mmid;";
		Map<String, Object> temp = this.baseDao.queryForMap(sql);
		
		if (temp != null) {
			commentCount = Integer.parseInt(temp.get("commentCount").toString()) - 1;
			starAvg = (Integer.parseInt(temp.get("totalStarValue").toString()) - comment.getStarValue()) / commentCount;
		}
		
		List<String> sqlList = new ArrayList<String>();
		sql = "delete from snowmm_mmcomment where mcid="+comment.getMcid()+"; ";
		sqlList.add(sql);
		sql = "update snowmm_mminfo set starAvg="+starAvg+",commentCount="+commentCount+" where mmid="+comment.getMmid()+";";
		sqlList.add(sql);
		
		this.baseDao.executeBatch(sqlList);
	}
	
	
	/**
	 * 雪花mm上线
	 * @param openId
	 */
	public void mmCheckin(int mmid, double latitude, double longitude, String label, String poi) {
		
		// 首先让mm先下线
		this.mmCheckout(mmid);
		
		String checkin = DateTimeUtil.currentTimeStr();
		
		String sql = "insert into snowmm_mmlocation(mmid,latitude,longitude,label,poi,checkinTime,`status`) "
				+ "values("+mmid+","+latitude+","+longitude+",'"+label+"','"+poi+"','"+checkin+"',0);";
		
		this.baseDao.execute(sql);
		
	}
	
	/**
	 * 雪花mm下线
	 * @param openId
	 */
	public void mmCheckout(int mmid) {
		
		SnowMMLocation loca = this.getCurrentLocation(mmid);
		
		if (loca == null) {
			return;
		}
		
		Date checkinTime = loca.getCheckinTime();
		Date checkoutTime = new Date(System.currentTimeMillis());
		String checkin = DateTimeUtil.Date2String(checkinTime);
		String checkout = DateTimeUtil.Date2String(checkoutTime);
		long onlineTime = DateTimeUtil.getTimeDifference(checkoutTime, checkinTime);
		
		String sql = "update snowmm_mmlocation set checkinTime='"+checkin+"', checkoutTime='"+checkout+"', "
				+ "onlineTime="+onlineTime+", status=1 where mlid="+loca.getMlid()+"";
		
		this.baseDao.execute(sql);
		
	}
	

	
	
	/**
	 * 获取某个mm的所有预订座位请求
	 * @param mmid
	 * @param status -1:全部  0:预订中 1:预订成功  2:预订失败  3:取消预订  4:过期
	 * @return
	 */
	public List<SnowMMPrebook> getPrebookList(int mmid, int status) {
		
		String condition = "1=1";
		if (status != -1) {
			condition = "b.status="+status;
		}
		
		String sql = "select b.id,b.openId,b.mmid,b.mlid,b.custNum,b.custName,b.custPhone,b.arriTime,b.status,b.createTime,b.updateTime, "
				+ "lo.latitude,lo.longitude,lo.label,lo.poi "
				+ "from snowmm_prebook b left join snowmm_mmlocation lo on b.mlid=lo.mlid "
				+ "where "+condition+" and b.mmid="+mmid+" order by b.createTime desc ";
		
		List<Map<String, Object>> tempList = this.baseDao.queryForList(sql);
		
		if (tempList == null || tempList.size() == 0) {
			return null;
		}
		
		List<SnowMMPrebook> bookList = new ArrayList<SnowMMPrebook>();
		for (Map<String, Object> temp : tempList) {
			SnowMMPrebook book = new SnowMMPrebook();
			book.setId(Integer.parseInt(temp.get("id").toString()));
			book.setOpenId(temp.get("openId").toString());
			book.setMmid(Integer.parseInt(temp.get("mmid").toString()));
			book.setMlid(Integer.parseInt(temp.get("mlid").toString()));
			book.setCustNum(Integer.parseInt(temp.get("custNum").toString()));
			book.setCustName(temp.get("custName")==null ? null : temp.get("custName").toString());
			book.setCustPhone(temp.get("custPhone").toString());
			book.setArriTime(temp.get("arriTime")==null ? null : temp.get("arriTime").toString());
			book.setStatus(Integer.parseInt(temp.get("status").toString()));
			book.setCreateTime(DateTimeUtil.String2Date2(temp.get("createTime").toString()));
			
			SnowMMLocation loca = new SnowMMLocation();
			loca.setLatitude(Double.parseDouble(temp.get("latitude").toString()));
			loca.setLongitude(Double.parseDouble(temp.get("longitude").toString()));
			loca.setLabel(temp.get("label").toString());
			loca.setPoi(temp.get("poi").toString());
			book.setLoca(loca);
			
			bookList.add(book);
		}
		
		return bookList;
	}
	
	
	
	/**
	 * 根据用户当前的位置，获取最近的前3个mm
	 * @param uloca
	 * @return
	 */
	public List<SnowMM> nearbyMM(UserLocation uloca) {
		
		double myLat = uloca.getLatitude();
		double myLong = uloca.getLongitude();
		int dist = ConfigInfo.snowmmNearbyDist;
		
		List<Map<String, Object>> tempList = null;
		
		for (int i = 0;; i++) {
			if (i == 0) {
				// 第一次搜索，范围是默认值
				dist = dist * 1;
			} else if (i == 1) {
				// 第二次搜索，范围扩大到10倍
				dist = dist * 10;
			} else if (i == 2) {
				// 第三次搜索，范围扩大到30倍
				dist = dist * 30;
			} else if (i == 3) {// 只有当返回的结果少于15条的情况下，才进行第四次搜索
				// 第四次搜索，范围扩大到60倍
				dist = dist * 60;
			} else {
				break;
			}
			
			Object[] params = new Object[] { myLat, myLong, dist };
			tempList = this.baseDao.find("call nearbymm(?,?,?);", params);
			
			if (tempList != null && tempList.size() > 0) {
				break;
			}
		}
		
		if (tempList == null || tempList.size() == 0) {
			return null;
		}
		
		List<SnowMM> mmList = new ArrayList<SnowMM>();
		
		for (Map<String, Object> temp : tempList) {
			SnowMM snowmm = new SnowMM();
			snowmm.setMmid(Integer.parseInt(temp.get("mmid").toString()));
			snowmm.setOpenId(temp.get("openId")==null ? null : temp.get("openId").toString());
			snowmm.setName(temp.get("name").toString());
			snowmm.setNickname(temp.get("nickname").toString());
			snowmm.setPhone(temp.get("phone")==null ? null : temp.get("phone").toString());
			snowmm.setAge(Integer.parseInt(temp.get("age").toString()));
			snowmm.setConstellation(temp.get("constellation").toString());
			snowmm.setIntroduce(temp.get("introduce").toString());
			snowmm.setHobby(temp.get("hobby").toString());
			snowmm.setPhotoUrl(temp.get("photoUrl").toString());
			snowmm.setPhotoUrlBig(temp.get("photoUrlBig").toString());
			snowmm.setSellDistrict(temp.get("sellDistrict").toString());
			snowmm.setSellOffice(temp.get("sellOffice").toString());
			snowmm.setRank(Integer.parseInt(temp.get("rank").toString()));
			snowmm.setStarAvg(Float.parseFloat(temp.get("starAvg").toString()));
			snowmm.setCommentCount(Integer.parseInt(temp.get("commentCount").toString()));
			snowmm.setDistance(Double.parseDouble(temp.get("distance").toString()));
			
			SnowMMLocation loca = null;
			if (temp.get("mlid") != null) {
				loca = new SnowMMLocation();
				loca.setMlid(Integer.parseInt(temp.get("mlid").toString()));
				loca.setMmid(Integer.parseInt(temp.get("mmid").toString()));
				loca.setLatitude(Double.parseDouble(temp.get("latitude").toString()));
				loca.setLongitude(Double.parseDouble(temp.get("longitude").toString()));
				loca.setLabel(temp.get("label").toString());
				loca.setPoi(temp.get("poi").toString());
				loca.setCheckinTime(DateTimeUtil.String2Date2(temp.get("checkinTime").toString()));
				loca.setCheckoutTime(temp.get("checkoutTime")==null ? null : DateTimeUtil.String2Date2(temp.get("checkoutTime").toString()));
				loca.setOnlineTime(temp.get("onlineTime")==null ? -1 : Integer.parseInt(temp.get("onlineTime").toString()));
				loca.setStatus(Integer.parseInt(temp.get("status").toString()));
			} 
			
			snowmm.setCurrLocation(loca);
			
			mmList.add(snowmm);
		}
		
		return mmList;
	}
}
